/*
 *说明：工单表 首页脚本
 *
 *@author mlc 2013-04-07
*/

var opeForm;
var grid;

$(function(){
	opeForm = $('#opeForm');

	grid=$('#listTable').datagrid({
		frozenColumns:[[
            {title:'选择',field:'ck',checkbox:true}
		]],
		columns:[[

            {title:'工单号',field:'order_id',align:'center',width:60},

            {title:'工单类型',field:'order_type_decode',align:'center',width:40},
            
            {title:'申请人',field:'apply_by_name',align:'center',width:60},

            {title:'申请时间',field:'apply_at',align:'center',width:80},
            
            {title:'工单状态',field:'order_status_decode',align:'center',width:60},            

            {title:'审批人',field:'approve_by_name',align:'center',width:60},

            {title:'审批时间',field:'approve_at',align:'center',width:80},
            
            {title:'审批意见',field:'approve_content',align:'center',width:80},

			{title:'操作',field:'operation',align:'center',width:100,
	        	formatter:function(value,rec){
         		var s = '<a style="cursor:pointer; color:blue" onclick=\'showInfo("' + rec.key + '")\'>查看明细</a>';
	        	    return s;
		   		}
		    }
        ]]
	});

	queryOperation();

	$('#queryBtn').click(queryOperation);
	$('#clearBtn').click(clearOperation);
	$('#deleteBtn').click(deleteOperation);
	$('#reloadBtn').click(refreshWin);
});

//翻页时自动调用此函数
function onLoadPrev(msg){
	var queryParams=grid.datagrid('options').queryParams;
	queryParams['firstQuery']=false;
}

//查询操作
function queryOperation(){
	var params = opeForm.serializeObject();
	grid.datagrid("options").pageNumber = 1;
	grid.datagrid({
		url: 'P_WF_ORDER!showList.do',
		queryParams: jQuery.extend({}, params, {'firstQuery': true})
	});
}

//清空操作（重置）
function clearOperation() {
	opeForm.resetForm();
}

//删除工单表
function deleteOperation() {

	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中要删除的记录!');	
		return;
	}

	var ids = [];

	for (var i = 0; i < rows.length; i++) {
		ids.push(rows[i].key);
	}

	var params = $ .param({'id' : ids}, true);

	$ .messager.confirm('提示信息', '您确定要删除选定的' + rows.length + '条记录吗?', function(result){
		if(result){
			$ .ajax({
				url: 'P_WF_ORDER!doDelete.do',
				data: params,
				type: 'post',
				success: function(data){
				    showInfoWin('操作成功!');
					afterOK();
				}
			});
		}	
	});
}

//查看明细
function showInfo(fid) {
	var href = 'P_WF_ORDER!showInfo.do?model.key=' + fid;
	openWin(href, 'pop', 'A0', '1150', '750');
}

//操作成功后刷新列表
function afterOK(){
	queryOperation();
}